package com.ficus.road.maintain.third.zh.maintenance;

import com.alibaba.fastjson.JSONObject;
import com.ficus.road.maintain.core.model.Hit;
import com.ficus.road.maintain.core.model.RoadTypeEnum;
import com.ficus.road.maintain.core.model.zh.maintenance.MaintenanceTimeoutZH;
import com.ficus.road.maintain.core.model.zh.maintenance.MaintenanceTmOut;
import com.ficus.road.maintain.dao.HitAllFactoryDao;
import com.ficus.road.maintain.dao.MaintenanceTmOutDao;
import com.ficus.road.maintain.strategy.allfactory.AllFactoryContext;
import com.ficus.road.maintain.strategy.allfactory.MaintenanceTmOutStrategy;
import com.ficus.road.maintain.util.MyDateUtil;
import com.ficus.road.maintain.third.zh.BaseZH;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

/**
 * @author xiawen yang
 * @date 2022/4/13 下午5:05
 */
@Component
public class ZHMaintenanceTmOut extends BaseZH {

    @Autowired
    private HitAllFactoryDao hitAllFactoryDao;
    @Autowired
    private MaintenanceTmOutDao maintenanceTmOutDao;

    @PostConstruct
    void init() {
        arg0 = "ytMaintenanceTmOut";
        errMsg = "调用中海接口获取维修超时异常!";
        successMsg = "调用中海接口获取维修超时成功!";
    }

    //    @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    @Scheduled(cron = "0 0 12,14,16 * * ?")
    public void refreshMaintenanceTmOut() {
//        Date startDate = MyDateUtil.strParseDate("2020-01", "yyyy-MM");
//        String maintenanceTmOutStr = null;
//        AllFactoryContext allFactoryContext = new AllFactoryContext(hitAllFactoryDao, AllFactoryTypeEnum.MAINTENANCE_TM_OUT);
//        for (int k = 0; k < 26; k++) {
//            startDate = DateUtils.addMonths(startDate, 1);
//            String dateStr = MyDateUtil.dateFormatStr(startDate, "yyyy-MM");
//            arg1 = "{\"month\":\"" + dateStr + "\"}";
//            int i = 0;
//            while (i < 10) {
//                maintenanceTmOutStr = getZHData();
//                if (ObjectUtils.isNotEmpty(maintenanceTmOutStr)) {
//                    break;
//                }
//                i++;
//            }
//            if (ObjectUtils.isEmpty(maintenanceTmOutStr)) {
//                continue;
//            }
//            MaintenanceTimeoutZH maintenanceTimeoutZH = JSONObject.parseObject(maintenanceTmOutStr, MaintenanceTimeoutZH.class);
//            List<MaintenanceTimeoutZH.MaintenanceTimeout> glsList = maintenanceTimeoutZH.getGlsList();
//            if (ObjectUtils.isEmpty(glsList)) {
//                continue;
//            }
//            glsList.forEach(item -> {
//                Hit hit = allFactoryContext.convert2AllFactoryHit(item);
//                hitAllFactoryDao.saveOrUpdate(hit);
//
//                MaintenanceTmOut maintenanceTmOut = MaintenanceTmOut.builder()
//                        .id(item.getUnid())
//                        .date(dateStr)
//                        .timeLimit(item.getLimittime())
//                        .duration(item.getDuration())
//                        .roadName(item.getRoadname())
//                        .stake(item.getStake())
//                        .projectType(item.getContype())
//                        .unit(item.getYhsection())
//                        .company(item.getConstractionunit())
//                        .startTime(item.getStarttime())
//                        .startPic(item.getPic().split(",")[0])
//                        .endTime(item.getEndtime())
//                        .endPic(item.getEndpic().split(",")[0])
//                        .type(RoadTypeEnum.HIGHWAY)
//                        .longitude(item.getLongitude())
//                        .latitude(item.getLatitude())
//                        .build();
//                maintenanceTmOutDao.saveOrUpdate(maintenanceTmOut);
//            });
//        }

        String maintenanceTmOutStr = null;
        String dateStr = MyDateUtil.dateFormatStr(new Date(), "yyyy-MM");
        AllFactoryContext allFactoryContext = new AllFactoryContext(new MaintenanceTmOutStrategy(hitAllFactoryDao));
//        AllFactoryContext allFactoryContext = new AllFactoryContext(hitAllFactoryDao, AllFactoryTypeEnum.MAINTENANCE_TM_OUT);
        arg1 = "{\"month\":\"" + dateStr + "\"}";
        int i = 0;
        while (i < 10) {
            maintenanceTmOutStr = getZHData();
            if (ObjectUtils.isNotEmpty(maintenanceTmOutStr)) {
                break;
            }
            i++;
        }
        if (ObjectUtils.isEmpty(maintenanceTmOutStr)) {
            return;
        }
        MaintenanceTimeoutZH maintenanceTimeoutZH = JSONObject.parseObject(maintenanceTmOutStr, MaintenanceTimeoutZH.class);
        List<MaintenanceTimeoutZH.MaintenanceTimeout> glsList = maintenanceTimeoutZH.getGlsList();
        if (ObjectUtils.isEmpty(glsList)) {
            return;
        }
        glsList.forEach(item -> {
            Hit hit = allFactoryContext.convert2AllFactoryHit(item);
            hitAllFactoryDao.saveOrUpdate(hit);
            MaintenanceTmOut maintenanceTmOut = MaintenanceTmOut.builder()
                    .id(item.getUnid())
                    .date(dateStr)
                    .timeLimit(item.getLimittime())
                    .duration(item.getDuration())
                    .roadName(item.getRoadname())
                    .stake(item.getStake())
                    .projectType(item.getContype())
                    .unit(item.getYhsection())
                    .company(item.getConstractionunit())
                    .startTime(item.getStarttime())
                    .startPic(item.getPic().split(",")[0])
                    .endTime(item.getEndtime())
                    .endPic(item.getEndpic().split(",")[0])
                    .type(RoadTypeEnum.HIGHWAY)
                    .longitude(item.getLongitude())
                    .latitude(item.getLatitude())
                    .build();
            maintenanceTmOutDao.saveOrUpdate(maintenanceTmOut);
        });
    }
}
